﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tenta_2014_12_14_5
{
    class LinkedList<TValue> where TValue : class
    {
        public Node<TValue> Head { get; set; }
        public Node<TValue> Tail { get; set; }
        private int table_size;

        public LinkedList(int table_size = 0)
        {
            this.table_size = table_size;
            Head = Tail = null;
        }

        public Node<TValue> Insert(TValue data, Node<TValue> node)
        {
            if (node.Next == null)
                Tail = Tail.Next = new Node<TValue>(data, null, Tail);
            else
                node.Next = node.Next.Prev = new Node<TValue>(data, node.Next, node);
            return node.Next;
        }

        public Node<TValue> InsertAtHead(TValue data)
        {
            if (Head == null)
                Head = Tail = new Node<TValue>(data, null, null);
            else
                Head = Head.Prev = new Node<TValue>(data, Head, null);
            return Head;
        }

        public void Print(int table_size)
        {
            Node<TValue> n = Head;
            while (n != null)
            {
                Console.WriteLine("Index: " + Math.Abs(n.Data.GetHashCode() % table_size) + " Data: " + n.Data.ToString());
                n = n.Next;
            }
        }
    }
}
